﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace MN.Framework.Log
{
    public class GeneralLogs
    {
        static int MaxBufferSize = 5000;
        static bool StackOverFlow;
        static List<LogBuffer> buffers = new List<LogBuffer>();
        static Dictionary<TypeOfLog, string> LogsColor = new Dictionary<TypeOfLog, string>() { { TypeOfLog.End, "Black" }, { TypeOfLog.Error, "Red" },
            { TypeOfLog.Info, "Black" }, { TypeOfLog.OK, "Green" }
            , { TypeOfLog.Other, "Gray" }, { TypeOfLog.Start, "Blue" }};

        static LogBuffer[] GetLogs()
        {
            LogBuffer[] buffersTemp = buffers.ToArray();
            buffers.Clear();
            return buffersTemp;
        }
        public static List<string> GetNewLogsAsHtml()
        {
            return GetLogs().Select(x => string.Format("[span style='color:{0}'] {1}: {2} | {3} [/span]",
                LogsColor[x.Type], x.CreationDate.ToShortTimeString(), x.Type, x.Value)).ToList();
        }
        public static List<string> GetNewLogs()
        {
            return GetLogs().Select(x => x.CreationDate.ToShortTimeString() + ": " + x.Type + "|" + x.Value).ToList();
        }
        //public static string[] getNewLogs(string Filter)
        //{
        //    string[] buffersTemp = buffers.Where(x => x.Value.ContainsX(Filter)).Select(x => x.Value).ToArray();
        //    buffers.RemoveAll(x => x.Value.ContainsX(Filter));
        //    return buffersTemp;
        //}
        #region WriteLog
        [Obsolete("WriteLog is deprecated, please use WriteLog by 3 args instead.")]
        public static void WriteLog(string value)
        {
            #region Console
            //if (Log.IndexOf("STARTING...") == 0)
            //{
            //    CLRConsole.ForegroundColor = ConsoleColor.Blue;
            //    CLRConsole.WriteLine(Log);
            //}
            //else if (Log.IndexOf(">Error") == 1 || Log.IndexOf(">Errror") == 0)
            //{
            //    CLRConsole.ForegroundColor = ConsoleColor.Red;
            //    CLRConsole.WriteLine(Log.Length < 150 ? Log : Log.Substring(0, 150));
            //}
            //else if (Log.IndexOf(">OK") == 1 || Log.IndexOf(">OK") == 0)
            //{
            //    CLRConsole.ForegroundColor = ConsoleColor.Yellow;
            //    CLRConsole.WriteLine(Log.Length < 150 ? Log : Log.Substring(0, 150));
            //}
            //else if (Log.IndexOf(">STOP") == 0)
            //{
            //    CLRConsole.ForegroundColor = ConsoleColor.Red;
            //    CLRConsole.WriteLine(Log);
            //    if (Log.IndexOf("STOPALL") == 1)
            //    {
            //        Console.ReadKey();
            //        return;
            //    }
            //}
            //else
            //{
            //    CLRConsole.ForegroundColor = ConsoleColor.White;
            //    CLRConsole.WriteLine(Log);
            //} 
            #endregion
            LogBuffer log = new LogBuffer();
            log.Value = value;
            log.CreationDate = DateTime.Now;
            if (value.Contains(">OK"))
                log.Type = TypeOfLog.OK;
            else if (value.Contains(">INFO"))
                log.Type = TypeOfLog.Info;
            else if (value.Contains(">Error"))
                log.Type = TypeOfLog.Error;

            WriteLog(log);
        }
        public static void WriteLog(string value, TypeOfLog LogType)
        {
            WriteLog(new LogBuffer { CreationDate = DateTime.Now, Type = LogType, Value = value });
        }
        private static void WriteLog(LogBuffer log)
        {

            buffers.Add(log);
            if (buffers.Count > MaxBufferSize)
            {
                StackOverFlow = true;
                buffers.Clear();
            }

        }
        //public static void WriteLogInDB(string Log)
        //{
        //    WriteLogInDB(Log, TypeOfLog.Other);
        //}
        //public static void WriteLogInDB(string Log, TypeOfLog LogType)
        //{
        //    WriteLog(Log);
        //    TazehaContext entiti = new TazehaContext();
        //    entiti.LogsBuffers.Add(new Tazeyab.Common.Models.LogsBuffer { Type = LogType, CreationDate = DateTime.Now, Value = Log });
        //    entiti.SaveChanges();
        //    if (buffers.Count > MaxBufferSize)
        //    {
        //        StackOverFlow = true;
        //        buffers.Clear();
        //    }

        //}
        #endregion
    }
}